{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# pip install scikit-learn --upgrade\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.datasets import load_digits\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.metrics import classification_report,confusion_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "digits = load_digits()#载入数据\n",
    "x_data = digits.data #数据\n",
    "y_data = digits.target #标签\n",
    "\n",
    "# 标准化\n",
    "scaler = StandardScaler()\n",
    "x_data = scaler.fit_transform(x_data)\n",
    "x_train,x_test,y_train,y_test = train_test_split(x_data,y_data) #分割数据1/4为测试数据，3/4为训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MLPClassifier(activation='relu', alpha=0.0001, batch_size='auto', beta_1=0.9,\n",
       "       beta_2=0.999, early_stopping=False, epsilon=1e-08,\n",
       "       hidden_layer_sizes=(100, 50), learning_rate='constant',\n",
       "       learning_rate_init=0.001, max_iter=500, momentum=0.9,\n",
       "       nesterovs_momentum=True, power_t=0.5, random_state=None,\n",
       "       shuffle=True, solver='adam', tol=0.0001, validation_fraction=0.1,\n",
       "       verbose=False, warm_start=False)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlp = MLPClassifier(hidden_layer_sizes=(100,50) ,max_iter=500)\n",
    "mlp.fit(x_train,y_train )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      1.00      1.00        46\n",
      "          1       0.94      0.96      0.95        52\n",
      "          2       0.98      1.00      0.99        44\n",
      "          3       1.00      0.97      0.99        39\n",
      "          4       1.00      0.95      0.97        41\n",
      "          5       1.00      1.00      1.00        41\n",
      "          6       1.00      1.00      1.00        50\n",
      "          7       0.96      1.00      0.98        44\n",
      "          8       0.95      0.95      0.95        44\n",
      "          9       0.98      0.96      0.97        49\n",
      "\n",
      "avg / total       0.98      0.98      0.98       450\n",
      "\n"
     ]
    }
   ],
   "source": [
    "predictions = mlp.predict(x_test)\n",
    "print(classification_report(y_test, predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[46  0  0  0  0  0  0  0  0  0]\n",
      " [ 0 50  0  0  0  0  0  0  1  1]\n",
      " [ 0  0 44  0  0  0  0  0  0  0]\n",
      " [ 0  0  1 38  0  0  0  0  0  0]\n",
      " [ 0  1  0  0 39  0  0  1  0  0]\n",
      " [ 0  0  0  0  0 41  0  0  0  0]\n",
      " [ 0  0  0  0  0  0 50  0  0  0]\n",
      " [ 0  0  0  0  0  0  0 44  0  0]\n",
      " [ 0  2  0  0  0  0  0  0 42  0]\n",
      " [ 0  0  0  0  0  0  0  1  1 47]]\n"
     ]
    }
   ],
   "source": [
    "print(confusion_matrix(y_test,predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
